using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

namespace dender.dvdcover.core
{
    /// <summary>
    /// A Part that can contain an image
    /// </summary>
    public class CoverPart : Part
    {
        public event EventHandler<PartImageChangedEventArgs> OnPartImageChanged;

        private CoverImage mImage;
        /// <summary>
        /// Gets the image of the part.
        /// </summary>
        [XmlIgnore()]
        public CoverImage Image
        {
            get { return mImage; }
        }

        public CoverPart(Part p) : base(p)
        {

        }

        /// <summary>
        /// Fills the part with a specified image
        /// </summary>
        /// <param name="image">Image used to fill the part</param>
        public void Fill(CoverImage image)
        {
            this.mImage = image;
            this.mImage.OnImageChanged += new EventHandler<ImageChangedEventArgs>(OnImageChanged);
            EventTrigger<PartImageChangedEventArgs>.Invoke(OnPartImageChanged, this, new PartImageChangedEventArgs(this.mImage));
        }

        /// <summary>
        /// Occurs when the ImageChangeEvent of the underlining CoverImage is triggered
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnImageChanged(object sender, ImageChangedEventArgs e)
        {
            EventTrigger<PartImageChangedEventArgs>.Invoke(OnPartImageChanged, this, new PartImageChangedEventArgs(this.mImage));
        }

        /// <summary>
        /// Clears the part, by removing the image
        /// </summary>
        public void Clear()
        {
            EventTrigger<PartImageChangedEventArgs>.Invoke(OnPartImageChanged, this, new PartImageChangedEventArgs(this.mImage));
            this.mImage = null;
        }
    }
}
